package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Personnel;
import cn.wolfcode.crm.query.PersonnelQueryObject;
import cn.wolfcode.crm.service.IPersonnelService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("personnel")
public class PersonnelController {

    @Autowired
    private IPersonnelService personnelService;

    @RequestMapping("")
    public String index(){
        return "personnel/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(PersonnelQueryObject qo){
        System.out.println(qo);
        return personnelService.query(qo);
    }


    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Personnel entity){
        JsonResult result = new JsonResult();
        try {
            personnelService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Personnel entity){

        JsonResult result = new JsonResult();
        try {
            personnelService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑操作失败");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            personnelService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新操作失败");
        }
        return result;
    }
}
